package com.lifeng.springevent.defaultevent;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.context.event.EventListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

/**
 * Created by lifeng on 2019/8/5.
 * ContextRefreshedEvent 监听Spring容器初始化完毕或者刷新时触发该事件；
 */
@Component
public class MyRefreshListener implements ApplicationListener<ContextRefreshedEvent>{
    private static final Logger log = LoggerFactory.getLogger(MyRefreshListener.class);

	@Override
	public void onApplicationEvent(ContextRefreshedEvent event) {
		System.out.println("refresh...");
		 if (event.getApplicationContext().getParent() != null){
	          //必须加上该判断，否则会系统初始化两次 
	         //在web 项目中（spring mvc），系统会存在两个容器，一个是root application context ,另一个就是我们自己的 projectName-servlet context（作为root application context的子容器）。 
		 }
		 ApplicationContext ac = event.getApplicationContext();
	}

}
